package com.ljd.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.ljd.music.service.AdminService;
import com.ljd.music.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;


    // 判断是否登录成功
    @RequestMapping(value="/admin/login/status",method = RequestMethod.POST)
    public Object loginStatus(HttpServletRequest  request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();

        String name = request.getParameter("name").trim();
        String password = request.getParameter("password");


        // 是否有用户
        boolean flag = adminService.verifyPassword(name, password);

        if(flag) {
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"登录成功");
            // 将用户名存入到服务器的session中
            session.setAttribute(Consts.NAME,name);
            return jsonObject;
        }

        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"用户名或密码错误");
        return jsonObject;
    }
}
